package minh.bac.wcbrazil2014.adapter;

import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.GridFavoriteObject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class FavoriteGridAdapter extends BaseAdapter{

	Context context;
	int resourse;
	List<GridFavoriteObject> objects;
	DisplayImageOptions displayImageOptions;
	LayoutInflater inflater;
	private MySQLiteManager mMySQLiteManager;
	public FavoriteGridAdapter(Context context, int resourse, List<GridFavoriteObject> objects, DisplayImageOptions displayImageOptions) 
	{		
		this.context = context;
		this.objects = objects;
		this.resourse = resourse;
		this.displayImageOptions = displayImageOptions;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMySQLiteManager = new MySQLiteManager(context);
	}	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return objects.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder ;
		if(convertView == null)
		{
			holder = new ViewHolder();
			convertView  = inflater.inflate(resourse, parent, false);
			holder.nameTeam = (TextView) convertView.findViewById(R.id.choose_favorite_nametv);
			holder.imageTeam = (ImageView) convertView.findViewById(R.id.choose_favorite_image);
			holder.checkbox = (CheckBox) convertView.findViewById(R.id.choose_favorite_checkboxs);
			
			convertView.setTag(holder);		
		}
		else
			holder = (ViewHolder) convertView.getTag();
		final int pos = position;
		
		holder.nameTeam.setText(objects.get(position).getBallTeamObj().getDoiBong());
		ImageLoader.getInstance().displayImage(objects.get(position).getLinkImg(), holder.imageTeam, displayImageOptions);
		holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					mMySQLiteManager.updateFavorite(objects.get(pos).getBallTeamObj().getDoiBong(), 1);
				}
				else
				{
					mMySQLiteManager.updateFavorite(objects.get(pos).getBallTeamObj().getDoiBong(), 0);
				}
					
			}
		});
		
		return convertView;
	}
	private class ViewHolder{
		TextView nameTeam;
		ImageView imageTeam;
		CheckBox checkbox;
	}

}
